/*
** egg.c for zappy in /home/guidet_m/TECK2/projets/zappy/server_zappy
** 
** Made by michael guidet
** Login   <guidet_m@epitech.net>
** 
** Started on  Tue Jul  5 10:24:10 2011 michael guidet
** Last update Tue Jul  5 10:24:14 2011 michael guidet
*/

#include "server.h"

void		egg_check(t_env *e)
{
  int		i;
  t_egg		*tmp2;

  for (i = 0; e->teams[i].name; i++)
    for (tmp2 = e->teams[i].eggs; tmp2; tmp2 = tmp2->next)
      {
	tmp2->time -= timeval_to_int(e->elapsed);
	if (tmp2->time <= 0)
	  egg_hatch(&e->teams[i], tmp2, e);
      }
}

void		egg_hatch(t_team *team, t_egg *egg, t_env *e)
{
  t_fd		*tp;
  t_egg		*tmp;
  t_player	*new;
  char		cmd2[BUF_SIZE];

  printf("%sUn oeuf a eclos\n%s", RED, DEFAUT);
  sprintf(cmd2, "eht %d\n", egg->id);
  for (tp = e->fd; tp; tp = tp->next)
    if (tp->type == GRAPH)
      add_string_to_list(&tp->send_list, cmd2);
  new = new_drone(egg->team, egg->x, egg->y, e->t);
  new->id  = egg->id;
  add_drone_to_team(team, new);
  team->dispo++;
  if (team->eggs == egg)
    {
      team->eggs = team->eggs->next;
      free(egg);
      return;
    }
  for (tmp = team->eggs; tmp->next; tmp = tmp->next)
    if (tmp->next == egg)
      break;
  tmp->next = tmp->next->next;
  free(egg);
}
